<?php
class Application_Model_dataDAO_DanhMucBanTinDAO implements Application_Model_dataDAOInf_DanhMucBanTinDAOInf {
	
	protected static $_instance = null;
	
	protected function __construct() {
	
	}
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}
	
	/**
	 * @param unknown_type $tenDanhMuc
	 * @param unknown_type $chuThich
	 */
	public function insert($tenDanhMuc, $chuThich) {
		$kq = 0;
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			$arrs = array ("tenDanhMuc" => $tenDanhMuc, "ChuThich" => $chuThich );
			
			$dao->insert ( 'tb_danhmucbantin', $arrs );
			$kq = $dao->lastInsertId ();
			$dao->closeConnection ();
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	
	}
	
	/**
	 * @param unknown_type $maDanhMuc
	 * @param unknown_type $tenDanhMuc
	 * @param unknown_type $chuThich
	 */
	public function update($maDanhMuc, $tenDanhMuc, $chuThich) {
		$kq = false;
		
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			
			$update = array ("maDanhMucBanTin" => $maDanhMuc, "tenDanhMuc" => $tenDanhMuc, "ChuThich" => $chuThich );
			$dk [] = 'maDanhMucBanTin=' . $maDanhMuc;
			
			$check = $dao->update ( 'tb_danhmucbantin', $update, $dk );
			if ($check == 1)
				$kq = true;
			
			$dao->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	}
	
	/**
	 * @param unknown_type $maDanhMuc
	 */
	public function delete($maDanhMuc) {
		$kq = false;
		
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			$dk [] = 'maDanhMucBanTin=' . $maDanhMuc;
			
			$check = $dao->delete ( 'tb_danhmucbantin', $dk );
			
			if ($check == 1)
				$kq = true;
			
			$dao->closeConnection ();
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	}
	
	/**
	 * 
	 */
	public function loadAll() {
		$kq = array ();
		
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			$sql = "SELECT * FROM tb_danhmucbantin";
			$rs = $dao->fetchAll ( $sql );
			$i = 0;
			
			foreach ( $rs as $item ) {
				$danhMuc = new Application_Model_bean_DanhMucBanTin ( $item ['maDanhMucBanTin'], $item ['tenDanhMuc'], $item ['ChuThich'] );
				$kq [$i] = $danhMuc;
				$i ++;
			}
			
			$dao->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	}
	
	/**
	 * @param unknown_type $maDanhMuc
	 */
	public function load($maDanhMuc) {
		$kq = null;
		
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			$sql = "SELECT * FROM tb_danhmucbantin WHERE maDanhMucBanTin=" . $maDanhMuc;
			
			$rs = $dao->fetchAll ( $sql );
			foreach ( $rs as $r ) :
				$maDanhMucBanTin = $r ['maDanhMucBanTin'];
				$tenDanhMuc = $r ['tenDanhMuc'];
				$chuThich = $r ['ChuThich'];
			endforeach
			;
			$kq = new Application_Model_bean_DanhMucBanTin ( $maDanhMucBanTin, $tenDanhMuc, $chuThich );
			$dao->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	}

}

?>
